package com.wwj.depository.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wwj.common.constant.CommonCodeEnum;
import com.wwj.common.constant.CommonConstant;
import com.wwj.common.entitiy.R;
import com.wwj.common.utils.TokenCheck;
import com.wwj.depository.constant.ConstantFinal;
import com.wwj.depository.entity.DepositoryExpressInformationEntity;
import com.wwj.depository.entity.DepositoryLocationEntity;
import com.wwj.depository.service.DepositoryLocationService;
import com.wwj.depository.service.ExpressService;
import com.wwj.depository.vo.ExpressVo;
import com.wwj.depository.vo.SignVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/expressForMini")
public class ExpressForMiniProgramController {

    @Resource
    private ExpressService expressService;

    @Resource
    private DepositoryLocationService depositoryLocationService;

    /**
     * 微信小程序获取当前用户的快递信息
     * http://127.0.0.1:88/api/expressForMini/getMyExpressData?query=xxx
     */
    @GetMapping("/getMyExpressData")
    private R<Object> getMyExpressData(HttpServletRequest request, @RequestParam("query") String query){
        String token = request.getHeader(CommonConstant.REQUEST_HEADER_TOKEN);
        // 解析该token的用户id
        String userId = TokenCheck.getUserIdByToken(token);
        // 查询该用户的快递信息
        QueryWrapper<DepositoryExpressInformationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ConstantFinal.FIELD_USER_ID, userId);
        queryWrapper.orderByAsc(ConstantFinal.FIELD_INFORMATION_SIGN_TIME);
        // 如果有查询语句就拼接查询语句
        if (!query.isEmpty()) {
            queryWrapper.like(ConstantFinal.FIELD_INFORMATION_NUMBER, query);
        }
        List<DepositoryExpressInformationEntity> list = expressService.list(queryWrapper);
        List<ExpressVo> expressVoList = new ArrayList<>();
        list.forEach(item -> {
            // 查询对应的库位名
            QueryWrapper<DepositoryLocationEntity> queryWrapperForLocation = new QueryWrapper<>();
            queryWrapperForLocation.eq(ConstantFinal.FIELD_LOCATION_ID, item.getLocationId());
            DepositoryLocationEntity locationEntity = depositoryLocationService.getOne(queryWrapperForLocation);
            ExpressVo expressVo = new ExpressVo();
            // 封装仓位名
            expressVo.setLocationName(locationEntity.getLocationName());
            BeanUtils.copyProperties(item, expressVo);
            expressVoList.add(expressVo);
        });
        return R.ok(CommonCodeEnum.SUCCESS.getCode(), CommonCodeEnum.SUCCESS.getMsg(), expressVoList);
    }

    /**
     *  微信小程序快递签收
     *  http://192.168.0.115:88/api/expressForMini/sign
     */
    @PutMapping("/sign")
    private R<Object> sign(HttpServletRequest request, @RequestBody SignVo signVo){
        String token = request.getHeader(CommonConstant.REQUEST_HEADER_TOKEN);
        // 解析该token的用户id
        String userId = TokenCheck.getUserIdByToken(token);
        // 1、查询出快递单号的详细信息
        QueryWrapper<DepositoryExpressInformationEntity> queryWrapper = new QueryWrapper<>();
        System.out.println("运单号：" + signVo.getInformationNumber());
        System.out.println("用户id：" + userId);
        queryWrapper.eq(ConstantFinal.FIELD_INFORMATION_NUMBER, signVo.getInformationNumber()).eq(ConstantFinal.FIELD_USER_ID, userId);
        DepositoryExpressInformationEntity one = expressService.getOne(queryWrapper);
        if (one == null){
            // 快递号不对
            return R.error(CommonCodeEnum.EXPRESS_NO_EXIST.getCode(), CommonCodeEnum.EXPRESS_NO_EXIST.getMsg());
        }
        // 2、获得locationId将库位改为不占用状态
        DepositoryLocationEntity locationEntity = new DepositoryLocationEntity(one.getLocationId(), null, ConstantFinal.VALUE_LOCATION_NO_STORAGE);
        depositoryLocationService.updateById(locationEntity);
        // 3、设置签收时间，更新快递表数据
        one.setInformationSignTime(new Date());
        expressService.updateById(one);
        return R.ok("签收成功");
    }

}
